package com.tanhua.server.Controller;

import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/movements")
@RestController
public class MovementsController {
    @Autowired
    private MovementsService movementsService;

    //发布动态
    @PostMapping
    public ResponseEntity movements(String textContent, MultipartFile imageContent[], String location, String longitude, String latitude) throws IOException {
        //调用service保存
        movementsService.movements(textContent, imageContent, location, latitude, longitude);
        return ResponseEntity.ok(null);
    }

    //查询我的动态
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              Long userId
    ) {
        //1. 调用service中的方法，返回一个包含该用户动态信息的一个集合
        PageResult vo = movementsService.all(page, pageSize, userId);
        //2. 返回数据
        return ResponseEntity.ok(vo);

    }

    //查询好友动态
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult vo = movementsService.findFriendId(page, pageSize);
        return ResponseEntity.ok(vo);

    }

    //查询推荐动态
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult vo = movementsService.findByRecommend(page, pageSize);
        return ResponseEntity.ok(vo);
    }

    //根据id查询动态
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String id) {
        MovementsVo vo = movementsService.findById(id);
        return ResponseEntity.ok(vo);
    }

    //首页：谁看过我功能（防bug）
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        return ResponseEntity.ok(null);
    }

    //点赞
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id) {
        //1. 接收参数（动态Id）
        //2. 调用service方法，返回点赞数量
        Integer count = movementsService.like(id);
        //3. 返回结果
        return ResponseEntity.ok(count);
    }

    //取消点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String id) {
        //1. 接收参数（动态Id）
        //2. 调用service方法，返回点赞数量
        Integer count = movementsService.dislike(id);
        //3. 返回结果
        return ResponseEntity.ok(count);
    }

    //喜欢
    @GetMapping("/{movementId}/love")
    public ResponseEntity love(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.love(movementId);
        return ResponseEntity.ok(count);
    }

     // 取消喜欢
    @GetMapping("/{movementId}/unlove")
    public ResponseEntity unlove(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.unlove(movementId);
        return ResponseEntity.ok(count);
    }

}
